
package com.kugou.game.sdk.adapter;

import com.kugou.game.sdk.base.BaseListAdapter;
import com.kugou.game.sdk.entity.Coupon;
import com.kugou.game.sdk.ui.activity.RechargeActivity;
import com.kugou.game.sdk.ui.dialog.BaseConfirmDialog;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 描述: 代金券列表适配器（横版）
 *
 * @author jamywang
 * @since 2016年5月19日 下午3:02:03
 */
public class CouponListAdapterLand extends BaseListAdapter<Coupon> implements OnClickListener {

    private int listType = 0;

    private Coupon couponLeft, couponRight;

    public CouponListAdapterLand(Context context, ViewGroup viewGroup) {
        super(context, viewGroup);
    }

    @Override
    public int getCount() {
        return mDatas.size() % 2 == 0 ? mDatas.size() / 2 : mDatas.size() / 2 + 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(Rx.layout.kg_coupon_list_item_land, null);
            holder = new ViewHolder();
            holder.backgroundLeft = (LinearLayout) convertView
                    .findViewById(Rx.id.kg_item_coupon_background_left);
            holder.tvCouponMoneyLeft = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_money_left);
            holder.tvMoneyUnitLeft = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_money_unit_left);
            holder.tvCouponNameLeft = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_name_left);
            holder.tvPlatfromLeft = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_platfrom_left);
            holder.tvEndDateLeft = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_date_left);
            holder.tvStatusLeft = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_status_left);
            holder.backgroundRight = (LinearLayout) convertView
                    .findViewById(Rx.id.kg_item_coupon_background_right);
            holder.tvCouponMoneyRight = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_money_right);
            holder.tvMoneyUnitRight = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_money_unit_right);
            holder.tvCouponNameRight = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_name_right);
            holder.tvPlatfromRight = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_platfrom_right);
            holder.tvEndDateRight = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_date_right);
            holder.tvStatusRight = (TextView) convertView
                    .findViewById(Rx.id.kg_tv_item_coupon_status_right);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position * 2 + 1 < mDatas.size()) {
            couponLeft = mDatas.get(position * 2);
            couponRight = mDatas.get(position * 2 + 1);
        } else if (position * 2 + 1 == mDatas.size()) {
            couponLeft = mDatas.get(position * 2);
            couponRight = null;
        }

        if (listType == 0) {

            if (couponLeft != null) {
                holder.backgroundLeft.setBackgroundResource(Rx.drawable.kg_coupon_item_usable);
                holder.backgroundLeft.setTag(couponLeft);
                holder.backgroundLeft.setOnClickListener(this);
                holder.tvStatusLeft.setVisibility(View.GONE);
                holder.tvCouponMoneyLeft.setText("" + couponLeft.getMoney());
                holder.tvCouponMoneyLeft.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_orange));
                holder.tvMoneyUnitLeft.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_orange));
                holder.tvCouponNameLeft.setText(couponLeft.getCouponName());
                holder.tvCouponNameLeft.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_black));
                if (couponLeft.getType() == 2) {
                    holder.tvPlatfromLeft.setText("限游币钱包充值");
                }
                if (couponLeft.getType() == 1) {
                    if (couponLeft.getGames() != null && (couponLeft.getGames()).size() == 0) {
                        holder.tvPlatfromLeft.setText("限安卓游戏");
                    }
                    if (couponLeft.getGames() != null && (couponLeft.getGames()).size() == 1) {
                        holder.tvPlatfromLeft.setText("限《" + couponLeft.getGames().get(0) + "》安卓版");
                    }
                    if (couponLeft.getGames() != null && (couponLeft.getGames()).size() > 1) {
                        holder.tvPlatfromLeft.setText("限《" + couponLeft.getGames().get(0) + "》等游戏");
                    }
                }
                if (couponLeft.getLockStatus() == Coupon.COUPON_STATUS_LOCK) {
                    holder.tvEndDateLeft.setText("订单提交中，"
                            + AppUtil.getDate("HH点mm分", couponLeft.getLockTime()) + "后可用");
                } else {
                    holder.tvEndDateLeft.setText("截止期："
                            + AppUtil.getDate("yyyy.MM.dd", couponLeft.getEndtime()));
                }
                holder.tvStatusLeft.setText("");
            }

            if (couponRight != null) {
                holder.backgroundRight.setVisibility(View.VISIBLE);
                holder.backgroundRight.setBackgroundResource(Rx.drawable.kg_coupon_item_usable);
                holder.backgroundRight.setTag(couponRight);
                holder.backgroundRight.setOnClickListener(this);
                holder.tvStatusRight.setVisibility(View.GONE);
                holder.tvCouponMoneyRight.setText("" + couponRight.getMoney());
                holder.tvCouponMoneyRight.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_orange));
                holder.tvMoneyUnitRight.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_orange));
                holder.tvCouponNameRight.setText(couponRight.getCouponName());
                holder.tvCouponNameRight.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_black));
                if (couponRight.getType() == 2) {
                    holder.tvPlatfromRight.setText("限游币钱包充值");
                }
                if (couponRight.getType() == 1) {
                    if (couponRight.getGames() != null && (couponRight.getGames()).size() == 0) {
                        holder.tvPlatfromRight.setText("限安卓游戏");
                    }
                    if (couponRight.getGames() != null && (couponRight.getGames()).size() == 1) {
                        holder.tvPlatfromRight.setText("限《" + couponRight.getGames().get(0)
                                + "》安卓版");
                    }
                    if (couponRight.getGames() != null && (couponRight.getGames()).size() > 1) {
                        holder.tvPlatfromRight.setText("限《" + couponRight.getGames().get(0)
                                + "》等游戏");
                    }
                }
                if (couponRight.getLockStatus() == Coupon.COUPON_STATUS_LOCK) {
                    holder.tvEndDateRight.setText("订单提交中，"
                            + AppUtil.getDate("HH点mm分", couponRight.getLockTime()) + "后可用");
                } else {
                    holder.tvEndDateRight.setText("截止期："
                            + AppUtil.getDate("yyyy.MM.dd", couponRight.getEndtime()));
                }
                holder.tvStatusRight.setText("");
            } else {
                holder.backgroundRight.setVisibility(View.INVISIBLE);
            }

        }

        if (listType == 1) {
            if (couponLeft != null) {
                holder.backgroundLeft.setBackgroundResource(Rx.drawable.kg_coupon_item_history);
                holder.backgroundLeft.setOnClickListener(null);
                holder.tvStatusLeft.setVisibility(View.VISIBLE);
                holder.tvCouponMoneyLeft.setText("" + couponLeft.getMoney());
                holder.tvCouponMoneyLeft.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_gray_999999));
                holder.tvMoneyUnitLeft.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_gray_999999));
                holder.tvCouponNameLeft.setText(couponLeft.getCouponName());
                holder.tvCouponNameLeft.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_gray_999999));
                if (couponLeft.getType() == 2) {
                    holder.tvPlatfromLeft.setText("限游币钱包充值");
                }
                if (couponLeft.getType() == 1) {
                    if (couponLeft.getGames() != null && (couponLeft.getGames()).size() == 0) {
                        holder.tvPlatfromLeft.setText("限安卓游戏");
                    }
                    if (couponLeft.getGames() != null && (couponLeft.getGames()).size() == 1) {
                        holder.tvPlatfromLeft.setText("限《" + couponLeft.getGames().get(0) + "》安卓版");
                    }
                    if (couponLeft.getGames() != null && (couponLeft.getGames()).size() > 1) {
                        holder.tvPlatfromLeft.setText("限《" + couponLeft.getGames().get(0) + "》等游戏");
                    }
                }
                holder.tvEndDateLeft.setText("截止期："
                        + AppUtil.getDate("yyyy.MM.dd", couponLeft.getEndtime()));
                if (couponLeft.getStatus() == 2) {
                    holder.tvStatusLeft.setText("已使用");
                } else if (couponLeft.getStatus() == 10) {
                    holder.tvStatusLeft.setText("已过期");
                } else {
                    holder.tvStatusLeft.setText("");
                }
            }

            if (couponRight != null) {
                holder.backgroundRight.setVisibility(View.VISIBLE);
                holder.backgroundRight.setBackgroundResource(Rx.drawable.kg_coupon_item_history);
                holder.backgroundRight.setOnClickListener(null);
                holder.tvStatusRight.setVisibility(View.VISIBLE);
                holder.tvCouponMoneyRight.setText("" + couponRight.getMoney());
                holder.tvCouponMoneyRight.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_gray_999999));
                holder.tvMoneyUnitRight.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_gray_999999));
                holder.tvCouponNameRight.setText(couponRight.getCouponName());
                holder.tvCouponNameRight.setTextColor(mContext.getResources().getColor(
                        Rx.color.kg_common_gray_999999));
                if (couponRight.getType() == 2) {
                    holder.tvPlatfromRight.setText("限游币钱包充值");
                }
                if (couponRight.getType() == 1) {
                    if (couponRight.getGames() != null && (couponRight.getGames()).size() == 0) {
                        holder.tvPlatfromRight.setText("限安卓游戏");
                    }
                    if (couponRight.getGames() != null && (couponRight.getGames()).size() == 1) {
                        holder.tvPlatfromRight.setText("限《" + couponRight.getGames().get(0)
                                + "》安卓版");
                    }
                    if (couponRight.getGames() != null && (couponRight.getGames()).size() > 1) {
                        holder.tvPlatfromRight.setText("限《" + couponRight.getGames().get(0)
                                + "》等游戏");
                    }
                }
                holder.tvEndDateRight.setText("截止期："
                        + AppUtil.getDate("yyyy.MM.dd", couponRight.getEndtime()));
                if (couponRight.getStatus() == 2) {
                    holder.tvStatusRight.setText("已使用");
                } else if (couponRight.getStatus() == 10) {
                    holder.tvStatusRight.setText("已过期");
                } else {
                    holder.tvStatusRight.setText("");
                }
            } else {
                holder.backgroundRight.setVisibility(View.INVISIBLE);
            }

        }

        return convertView;
    }

    public static class ViewHolder {

        /**
         * 左边item
         */

        // 代金券布局
        public LinearLayout backgroundLeft;

        // 代金券金额
        public TextView tvCouponMoneyLeft;

        // 代金券金额单位
        public TextView tvMoneyUnitLeft;

        // 代金券名称
        public TextView tvCouponNameLeft;

        // 适用平台
        public TextView tvPlatfromLeft;

        // 截止日期
        public TextView tvEndDateLeft;

        // 代金券状态
        public TextView tvStatusLeft;

        /**
         * 右边item
         */

        // 代金券布局
        public LinearLayout backgroundRight;

        // 代金券金额
        public TextView tvCouponMoneyRight;

        // 代金券金额单位
        public TextView tvMoneyUnitRight;

        // 代金券名称
        public TextView tvCouponNameRight;

        // 适用平台
        public TextView tvPlatfromRight;

        // 截止日期
        public TextView tvEndDateRight;

        // 代金券状态
        public TextView tvStatusRight;

    }

    public int getListType() {
        return listType;
    }

    public void setListType(int listType) {
        this.listType = listType;
    }

    @Override
    public void onClick(View v) {
        Coupon couponInfo = (Coupon) v.getTag();
        int payType = couponInfo.getType();
        String tips = "";
        // 游戏代金券
        if (payType == 1) {
            // 所有游戏都可使用的代金券
            if (couponInfo.getGames() != null && (couponInfo.getGames()).size() == 0) {
                tips = "该代金券适用于酷狗游戏中心下载的所有安卓游戏，请尽快在游戏中支付使用哦。";
                showNoticeDialog(tips);
            }
            // 部分游戏可使用的代金券
            else if (couponInfo.getGames() != null && (couponInfo.getGames()).size() > 0) {
                String str = "";
                for (int i = 0; i < couponInfo.getGames().size(); i++) {
                    str += "《" + couponInfo.getGames().get(i) + "》、";
                }
                str = str.substring(0, str.length() - 1);
                tips = "该代金券仅使用于酷狗游戏中心下载的安卓版" + str + ",请尽快在游戏中支付使用哦。";
                showNoticeDialog(tips);
            }

        }

        // 游币代金券
        if (payType == 2) {
            tips = "该代金券仅适用于酷狗游币钱包充值，请尽快到游币钱包充值使用哦。";
            showRechargeDialog(tips);
        }
    }

    /**
     * 显示代金券使用提示的确定弹框
     * 
     * @param tips 弹框Message
     */
    private void showNoticeDialog(String tips) {
        final BaseConfirmDialog tipsDialog = new BaseConfirmDialog((Activity) mContext);
        tipsDialog
                .setOkBtnBackground(Rx.drawable.kg_selector_bottom_single_filleted_corner_retangle);
        tipsDialog.setMessage(TextUtils.isEmpty(tips) ? "" : tips);
        tipsDialog.setCancelBtnVisibility(View.GONE);
        tipsDialog.setContentGravity(Gravity.CENTER);
        tipsDialog.setOKBtnText("知道了");
        tipsDialog.setOKClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tipsDialog.dismiss();

            }
        });
        tipsDialog.show();
    }

    private void showRechargeDialog(String tips) {
        final BaseConfirmDialog dialog = new BaseConfirmDialog((Activity) mContext);
        dialog.setMessage(TextUtils.isEmpty(tips) ? "" : tips);
        dialog.setContentGravity(Gravity.CENTER);
        dialog.setOKBtnText("游币充值");
        dialog.setCancelText("关闭");
        dialog.setCancelClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setOKClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RechargeActivity.showYoubiRechargePage(mContext, 6);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

}
